package net.bwie.realtime.vehicle.controller;

import com.alibaba.fastjson.JSON;
import net.bwie.realtime.vehicle.bean.*;
import net.bwie.realtime.vehicle.service.VehicleRealtimeService;
import net.bwie.realtime.vehicle.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: FuHe
 * @Date: 2025/5/27
 */
@RestController
@RequestMapping("/vehicle/realtime")
public class VehicleRealtimeController {
    @Autowired
    VehicleRealtimeService vehicleRealtimeService;
    private static final String DATE_DEFAULT_VALUE = "1970-07-01";

    /**
     * todo 实时在线车辆数
     */
    @RequestMapping("/Uvcount")
    public String getVinUvCount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date) {

        // 请求参数日期，初步判断，默认值为当天日期
        if (DATE_DEFAULT_VALUE.equals(date)) {
            date = DateUtil.nowDate();
        }
//        // 请求服务层 ，查询数据
        Double vehicleUvReportList = vehicleRealtimeService.getUvCount(date);
        // 创建返回结果的Map
        Map<String, Object> result = new HashMap<>();
        // 如果没有数据，返回0
        if (vehicleUvReportList == null) {
            result.put("data", 0);
            result.put("status", 0);
        } else {
            result.put("status", 0);
            result.put("data", vehicleUvReportList);
        }
        // 构建json字符串
        return JSON.toJSONString(result);
    }

    /**
     * todo 平均电池健康度
     */
    @RequestMapping("/Health")
    public String getVinHealth(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date) {

        // 请求参数日期，初步判断，默认值为当天日期
        if (DATE_DEFAULT_VALUE.equals(date)) {
            date = DateUtil.nowDate();
        }
//        // 请求服务层 ，查询数据
        Double vehicleHealthReportList = vehicleRealtimeService.getVinHealth(date);
        // 创建返回结果的Map
        Map<String, Object> result = new HashMap<>();
        // 如果没有数据，返回0
        if (vehicleHealthReportList == null) {
            result.put("data", 0);
            result.put("status", 0);
        } else {
            result.put("status", 0);
            result.put("data", vehicleHealthReportList);
        }
        // 构建json字符串
        return JSON.toJSONString(result);
    }

    /**
     * todo 车辆地理分布
     */
    @RequestMapping("/Province")
    public String getVinProvince(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date) {

        // 请求参数日期，初步判断，默认值为当天日期
        if (DATE_DEFAULT_VALUE.equals(date)) {
            date = DateUtil.nowDate();
        }
//        // 请求服务层 ，查询数据
        List<VehicleProvinceCount> vehicleProReportList = vehicleRealtimeService.getVinProvince(date);
        if (vehicleProReportList == null || vehicleProReportList.isEmpty()) {
            return "[]";
        }
        Map<String, Object> map = new HashMap<>();
        map.put("mapData", vehicleProReportList);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("status", 0);
        resMap.put("data", map);
        // 构建json字符串
        return JSON.toJSONString(resMap);
    }

    /**
     * todo 行程识别与分析
     */
    @RequestMapping("/trip")
    public String getVinTrip(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date) {

        // 请求参数日期，初步判断，默认值为当天日期
        if (DATE_DEFAULT_VALUE.equals(date)) {
            date = DateUtil.nowDate();
        }
        // 请求服务层 ，查询数据
        List<VehicleTrip> vehicleTripReportList = vehicleRealtimeService.getVinTrip(date);
        if (vehicleTripReportList == null || vehicleTripReportList.isEmpty()) {
            return "[]";
        }
        ArrayList<SugarTwo> name = new ArrayList<>();
        name.add(new SugarTwo("车辆VIN", "vin"));
        name.add(new SugarTwo("行驶里程", "distance"));
        name.add(new SugarTwo("行驶时长", "drivingDuration"));
        name.add(new SugarTwo("平均速度", "avgSpeed"));
        name.add(new SugarTwo("最高速度", "maxSpeed"));
        name.add(new SugarTwo("超速行驶", "speeding"));

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"hitSugarSelf\": true,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": " +
                JSON.toJSONString(name)
                + ",\n" +
                "    \"rows\": " +
                JSON.toJSONString(vehicleTripReportList)
                + "\n" +
                "  }\n" +
                "}";
    }

    /**
     * todo 实时车辆状态
     */
    @RequestMapping("/Status")
    public String getVinRealtime(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date) {

        // 请求参数日期，初步判断，默认值为当天日期
        if (DATE_DEFAULT_VALUE.equals(date)) {
            date = DateUtil.nowDate();
        }
        List<VehicleStatus> carRealtimeList = vehicleRealtimeService.getVehicleConditionMonitoring(date);

        if (carRealtimeList == null || carRealtimeList.isEmpty()) {
            return "[]";
        }

        ArrayList<SugarTwo> name = new ArrayList<>();
        name.add(new SugarTwo("车辆编号", "vin"));
        name.add(new SugarTwo("车速", "speed"));
        name.add(new SugarTwo("电池电量", "batteryLevel"));
        name.add(new SugarTwo("电池温度", "batteryTemp"));
        name.add(new SugarTwo("电机温度", "motorTemp"));
        name.add(new SugarTwo("充电状态", "chargingStatus"));
        name.add(new SugarTwo("能耗", "energyConsumption"));

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"hitSugarSelf\": true,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": " +
                JSON.toJSONString(name)
                + ",\n" +
                "    \"rows\": " +
                JSON.toJSONString(carRealtimeList)
                + "\n" +
                "  }\n" +
                "}";
    }

    /**
     * todo 异常检测与告警  饼图
     */
    @RequestMapping("/Alarm")
    public String getVinAlarm(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date) {

        // 请求参数日期，初步判断，默认值为当天日期
        if (DATE_DEFAULT_VALUE.equals(date)) {
            date = DateUtil.nowDate();
        }
        // 请求服务层 ，查询数据
        List<VehicleProvinceCount> vehicleAlarmReportList = vehicleRealtimeService.getVinAlarm(date);
        if (vehicleAlarmReportList == null || vehicleAlarmReportList.isEmpty()) {
            return "[]";
        }
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("status", 0);
        resMap.put("data", vehicleAlarmReportList);
        // 构建json字符串
        return JSON.toJSONString(resMap);
    }

    /**
     * todo 异常检测与告警 轮播表格
     */
    @RequestMapping("/Alarm2")
    public String getVinAlarm2(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date) {

        // 请求参数日期，初步判断，默认值为当天日期
        if (DATE_DEFAULT_VALUE.equals(date)) {
            date = DateUtil.nowDate();
        }
        List<VehicleAlarm> carRealtimeList = vehicleRealtimeService.getVinAlarm2(date);

        if (carRealtimeList == null || carRealtimeList.isEmpty()) {
            return "[]";
        }

        ArrayList<SugarTwo> name = new ArrayList<>();
        name.add(new SugarTwo("车辆编号", "vin"));
        name.add(new SugarTwo("异常告警", "alarmType"));

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"hitSugarSelf\": true,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": " +
                JSON.toJSONString(name)
                + ",\n" +
                "    \"rows\": " +
                JSON.toJSONString(carRealtimeList)
                + "\n" +
                "  }\n" +
                "}";
    }
    /**
     * todo 电池健康度
     */
//    @RequestMapping("/SpeedLine")
//    public String getVinSpeed(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date) {
//
//        // 请求参数日期，初步判断，默认值为当天日期
//        if (DATE_DEFAULT_VALUE.equals(date)) {
//            date = DateUtil.nowDate();
//        }
//        List<VehicleAlarm> carRealtimeList = vehicleRealtimeService.getVinSpeed(date);
//
//        if (carRealtimeList == null || carRealtimeList.isEmpty()) {
//            return "[]";
//        }
//
//        ArrayList<SugarTwo> name = new ArrayList<>();
//        name.add(new SugarTwo("车辆编号", "vin"));
//        name.add(new SugarTwo("异常告警", "alarmType"));
//
//        return "{\n" +
//                "  \"status\": 0,\n" +
//                "  \"hitSugarSelf\": true,\n" +
//                "  \"msg\": \"\",\n" +
//                "  \"data\": {\n" +
//                "    \"columns\": " +
//                JSON.toJSONString(name)
//                + ",\n" +
//                "    \"rows\": " +
//                JSON.toJSONString(carRealtimeList)
//                + "\n" +
//                "  }\n" +
//                "}";
//    }
    /**
     * todo 每个车辆的车速
     */
    @RequestMapping("/SpeedLine")
    public String getVinSpeed(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date) {

        // 请求参数日期，初步判断，默认值为当天日期
        if (DATE_DEFAULT_VALUE.equals(date)) {
            date = DateUtil.nowDate();
        }
        List<VehicleVinSpeed> vehicleVinSpeedRealtimeList = vehicleRealtimeService.getVinSpeed(date);

        if (vehicleVinSpeedRealtimeList == null || vehicleVinSpeedRealtimeList.isEmpty()) {
            return "[]";
        }
        List<String> Camera_id = vehicleVinSpeedRealtimeList.stream().map(VehicleVinSpeed::getName).collect(Collectors.toList());
        List<Double> License_plate_count = vehicleVinSpeedRealtimeList.stream().map(VehicleVinSpeed::getValue).collect(Collectors.toList());
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"hitSugarSelf\": true,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": "+
                JSON.toJSONString(Camera_id)
                +",\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"速度\",\n" +
                "        \"data\": "+
                JSON.toJSONString(License_plate_count)
                +"\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }
    /**
     * todo 每个车辆的行驶里程
     */
    @RequestMapping("/Distance")
    public String getVinDistance(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date) {

        // 请求参数日期，初步判断，默认值为当天日期
        if (DATE_DEFAULT_VALUE.equals(date)) {
            date = DateUtil.nowDate();
        }
        List<VehicleVinSpeed> vehicleVinDistanceRealtimeList = vehicleRealtimeService.getVinDistance(date);

        if (vehicleVinDistanceRealtimeList == null || vehicleVinDistanceRealtimeList.isEmpty()) {
            return "[]";
        }
        List<String> Camera_id = vehicleVinDistanceRealtimeList.stream().map(VehicleVinSpeed::getName).collect(Collectors.toList());
        List<Double> License_plate_count = vehicleVinDistanceRealtimeList.stream().map(VehicleVinSpeed::getValue).collect(Collectors.toList());
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"hitSugarSelf\": true,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": "+
                JSON.toJSONString(Camera_id)
                +",\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"行驶里程\",\n" +
                "        \"data\": "+
                JSON.toJSONString(License_plate_count)
                +"\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }
    /**
     * todo 车辆异常告警
     */
    @RequestMapping("/getVehicleHealth")
    private String getVehicleHealth(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date) {
        // 请求参数日期，初步判断，默认值为当天日期
        if (DATE_DEFAULT_VALUE.equals(date)) {
            date = DateUtil.nowDate();
        }
        List<SugarOne> VehicleStatusList = vehicleRealtimeService.getVehicleHealth(date);
        if (VehicleStatusList == null || VehicleStatusList.isEmpty()) {
            return "[]";
        }
        List<String> Camera_id = VehicleStatusList.stream().map(SugarOne::getName).collect(Collectors.toList());
        List<String> License_plate_count = VehicleStatusList.stream().map(SugarOne::getValue).collect(Collectors.toList());
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"hitSugarSelf\": true,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": "+
                JSON.toJSONString(Camera_id)
                +",\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"健康度\",\n" +
                "        \"data\": "+
                JSON.toJSONString(License_plate_count)
                +"\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }

}
